﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using CommunitySystem.PublicOfficialSelectionService;
using CommunitySystem.Views;
using CommunitySystem.ZhaoLuService;
using org.in2bits.MyXls;

namespace CommunitySystem
{
    public partial class ZhaoLuKaoGuanDetailControl : UserControl
    {
        private PublicOfficialSelectionServiceClient Client = new PublicOfficialSelectionServiceClient();
        private KaoGuanDetailModel model = new KaoGuanDetailModel();

        public ObservableCollection<ZhaoLuJiHuaModel> poi = new ObservableCollection<ZhaoLuJiHuaModel>();
        private ZhaoLuService.ZhaoLuServiceClient Client1 = new ZhaoLuServiceClient();
        private ObservableCollection<KaoGuanModel> tempforall1 = new ObservableCollection<KaoGuanModel>();
        public int tiaojian = 0;
        private readonly MouseClickManager _gridClickManagerXQ;
        public ZhaoLuKaoGuanDetailControl()
        {
            InitializeComponent();
            //Client1.GetZhaoLuJiHuaModelCompleted += new EventHandler<GetZhaoLuJiHuaModelCompletedEventArgs>(Client1_GetZhaoLuJiHuaModelCompleted);
            //Client1.GetZhaoLuJiHuaModelAsync(0, "");
            Client1.GetZhaoLuJiHuaModelForComCompleted += new EventHandler<GetZhaoLuJiHuaModelForComCompletedEventArgs>(Client1_GetZhaoLuJiHuaModelForComCompleted);
            Client1.GetZhaoLuJiHuaModelForComAsync();
            _gridClickManagerXQ = new MouseClickManager(DGRole, 300);
            _gridClickManagerXQ.DoubleClick += new System.Windows.Input.MouseButtonEventHandler(_gridClickManagerXQ_DoubleClick);
        }
        private void dgXQ_LoadingRow(object sender, DataGridRowEventArgs e)
        {
            e.Row.MouseLeftButtonUp += _gridClickManagerXQ.HandleClick;
        }
        void Client1_GetZhaoLuJiHuaModelForComCompleted(object sender, GetZhaoLuJiHuaModelForComCompletedEventArgs e)
        {
            poi = e.Result;
            ObservableCollection<ZhaoLuJiHuaModel> poi1 = new ObservableCollection<ZhaoLuJiHuaModel>();
            poi1.Add(poi.Where(p => p.IsLocked == false).FirstOrDefault());
            nian.ItemsSource = poi1;
            if (poi1.Count > 0) nian.SelectedIndex = 0;
            Load();
        }

        private void _gridClickManagerXQ_DoubleClick(object sender, MouseButtonEventArgs e)
        {
            var seljw = this.DGRole.SelectedItem as KaoGuanModel;
            ZhaoLuKaoGuanDetailWindow RMWindow = new ZhaoLuKaoGuanDetailWindow(Convert.ToInt32(seljw.ID),tiaojian);
            RMWindow.Closed += new EventHandler(RMWindow_Closed);
            RMWindow.Show();
        }

        void Client1_GetZhaoLuJiHuaModelCompleted(object sender, GetZhaoLuJiHuaModelCompletedEventArgs e)
        {
            if (e.Result != null)
                poi = e.Result;
            nian.ItemsSource = poi;
            if (poi.Count > 0) nian.SelectedIndex = 0;
            Load();
        }

        public void Load()
        {
            Client.GetKaoGuanDetailModelCompleted += new EventHandler<GetKaoGuanDetailModelCompletedEventArgs>(Client_GetKaoGuanDetailModelCompleted);
            Client.GetKaoGuanDetailModelAsync(name.Text, tiaojian, 0, "招录工作");
        }
        private string jhname = "";
        void gangwei_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (((ComboBox)sender).SelectedItem != null)
            {
                tiaojian = ((ZhaoLuJiHuaModel)(((ComboBox)sender).SelectedItem)).ID;
                jhname = ((ZhaoLuJiHuaModel)(((ComboBox)sender).SelectedItem)).Name;
            }
        }

        private string islock = "0";
        private void CheckBox_Checked(object sender, RoutedEventArgs e)
        {
            islock = "1";
            ObservableCollection<ZhaoLuJiHuaModel> poi1 = new ObservableCollection<ZhaoLuJiHuaModel>();
            foreach (ZhaoLuJiHuaModel i in poi)
            {
                if (i.IsLocked) poi1.Add(i);
            }
            nian.ItemsSource = poi1;
            if (poi1.Count > 0) nian.SelectedIndex = 0;
            Load();
        }

        private void chkHiddenCols_Unchecked(object sender, RoutedEventArgs e)
        {
            islock = "0";
            ObservableCollection<ZhaoLuJiHuaModel> poi1 = new ObservableCollection<ZhaoLuJiHuaModel>();
            if (poi.Where(p => p.IsLocked == false).FirstOrDefault() != null) poi1.Add(poi.Where(p => p.IsLocked == false).FirstOrDefault());
            if (poi1.Count > 0 && poi1[0] != null)
            {
                nian.ItemsSource = poi1;
                if (poi1.Count > 0) nian.SelectedIndex = 0;
            }
            else
            {
                nian.ItemsSource = null;
                tiaojian = 0;
            }
            Load();
        }

        void Client_GetKaoGuanDetailModelCompleted(object sender, GetKaoGuanDetailModelCompletedEventArgs e)
        {
            DGRole.ItemsSource = null;
            tempforall1 = e.Result;
            DGRole.ItemsSource = e.Result;
        }
        /// <summary>
        /// 编辑考官其他信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void NewData_Click(object sender, RoutedEventArgs e)
        {
            var seljw = this.DGRole.SelectedItem as KaoGuanModel;

            //throw new NotImplementedException();
            if (seljw != null)
            {
                ZhaoLuKaoGuanDetailWindow RMWindow = new ZhaoLuKaoGuanDetailWindow(Convert.ToInt32(seljw.ID),tiaojian);
                RMWindow.Closed += new EventHandler(RMWindow_Closed);
                RMWindow.Show();

            }
            else SilverlightMessageBoxLibrary.Message.ErrorMessage("请选择要编辑的考官信息！");
        }
        void RMWindow_Closed(object sender, EventArgs e)
        {
            Load();
        }
        /// <summary>
        /// 批量设置考官类别
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ibtnPZ_Click(object sender, RoutedEventArgs e)
        {
            if (DGRole.ItemsSource != null)
            {
                foreach (KaoGuanModel i in (ObservableCollection<KaoGuanModel>)DGRole.ItemsSource)
                {
                    if (i.IsSelected == true && selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault() == null)
                    {
                        selectedempmodel.Add(i);
                    }
                    if (selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault() != null && i.IsSelected == false)
                    {
                        selectedempmodel.Remove(selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault());
                    }
                }
            }
            if (selectedempmodel.Count > 0)
            {
                
                    string ids = "";

                    foreach (KaoGuanModel i in selectedempmodel)
                    {
                        ids += i.ID.ToString() + ",";
                    }
                    SetType_ZhaoLuKaoGuanWindow RMWindow = new SetType_ZhaoLuKaoGuanWindow(ids.TrimEnd(','));
                    RMWindow.Show();
                    RMWindow.Closed +=new EventHandler(RMWindow_Closed);
            }else
                SilverlightMessageBoxLibrary.Message.ErrorMessage("请选择要配置的考官！");
          }

      /// <summary>
      /// 删除考官
      /// </summary>
      /// <param name="sender"></param>
      /// <param name="e"></param>
        private void DeleteData_Click(object sender, RoutedEventArgs e)
        {
            var seljw = this.DGRole.SelectedItem as KaoGuanModel;
            if (MessageBox.Show("确定删除吗？", "提示", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
            {
                Client.DeteleKaoGuanDetailModelCompleted -= new EventHandler<DeteleKaoGuanDetailModelCompletedEventArgs>(Client_DeteleKaoGuanDetailModelCompleted);
                Client.DeteleKaoGuanDetailModelCompleted += new EventHandler<DeteleKaoGuanDetailModelCompletedEventArgs>(Client_DeteleKaoGuanDetailModelCompleted);
                Client.DeteleKaoGuanDetailModelAsync(Convert.ToInt32(seljw.ID));
            }
        }

        void Client_DeteleKaoGuanDetailModelCompleted(object sender, DeteleKaoGuanDetailModelCompletedEventArgs e)
        {
            Load();
        }

        private void ChaData_Click(object sender, RoutedEventArgs e)
        {
            Load();
        }
        private Stream ss = null;
        /// <summary>
        /// 导出选中数据  XlsDocument
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Export_Click(object sender, RoutedEventArgs e)
        {
            if(tempforall1.Count > 0)
            {
                var downLoad = new SaveFileDialog()
                {
                    Filter = "Excel files(2003) (*.xls)|*.xls",
                };
                if (downLoad.ShowDialog() == true)
                {
                    ss = downLoad.OpenFile();
                    PageInit2();
                }
            }
            
        }

        public void PageInit2()
        {
            XlsDocument xls = new XlsDocument();
            XF xf = xls.NewXF();
            xf.HorizontalAlignment = HorizontalAlignments.Centered;
            xf.VerticalAlignment = VerticalAlignments.Centered;
            xf.UseBorder = true;
            xf.TopLineStyle = 1;
            xf.TopLineColor = org.in2bits.MyXls.Colors.Black;
            xf.BottomLineStyle = 1;
            xf.BottomLineColor = org.in2bits.MyXls.Colors.Black;
            xf.LeftLineStyle = 1;
            xf.LeftLineColor = org.in2bits.MyXls.Colors.Black;
            xf.RightLineStyle = 1;
            xf.RightLineColor = org.in2bits.MyXls.Colors.Black;
            xf.Font.FontName = "宋体"; // 字体 
            xf.TextWrapRight = true;

            XF xf1 = xls.NewXF();
            xf1.UseBorder = true;
            xf1.TopLineStyle = 1;
            xf1.TopLineColor = org.in2bits.MyXls.Colors.Black;
            xf1.BottomLineStyle = 1;
            xf1.BottomLineColor = org.in2bits.MyXls.Colors.Black;
            xf1.LeftLineStyle = 1;
            xf1.LeftLineColor = org.in2bits.MyXls.Colors.Black;
            xf1.RightLineStyle = 1;
            xf1.RightLineColor = org.in2bits.MyXls.Colors.Black;
            xf1.HorizontalAlignment = HorizontalAlignments.Centered;
            xf1.VerticalAlignment = VerticalAlignments.Centered;
            xf1.Font.Bold = true;
            xf1.Font.Height = 15 * 20;

            XF xf2 = xls.NewXF();
            xf2.UseBorder = true;
            xf2.TopLineStyle = 1;
            xf2.TopLineColor = org.in2bits.MyXls.Colors.Black;
            xf2.BottomLineStyle = 1;
            xf2.BottomLineColor = org.in2bits.MyXls.Colors.Black;
            xf2.LeftLineStyle = 1;
            xf2.LeftLineColor = org.in2bits.MyXls.Colors.Black;
            xf2.RightLineStyle = 1;
            xf2.RightLineColor = org.in2bits.MyXls.Colors.Black;
            xf2.HorizontalAlignment = HorizontalAlignments.Centered;
            xf2.VerticalAlignment = VerticalAlignments.Centered;
            xf2.Font.Bold = true;
            xf2.Font.FontName = "黑体"; // 字体  
            xf2.Font.Height = 12 * 20;
            xf2.TextWrapRight = true;

            Worksheet sheet1 = xls.Workbook.Worksheets.AddNamed("考官信息汇总表");
            sheet1.Cells.Merge(1, 1, 1, 13);
            sheet1.Cells.Add(1, 1, "徐汇区" + DateTime.Today.Year + "年科级及科以下公务员招录工作考官信息汇总表", xf1);
            sheet1.Cells.Add(2, 1, "序号", xf2);
            sheet1.Cells.Add(2, 2, "类别", xf2);
            sheet1.Cells.Add(2, 3, "考官类别", xf2);
            sheet1.Cells.Add(2, 4, "用人单位", xf2);
            sheet1.Cells.Add(2, 5, "姓名", xf2);
            sheet1.Cells.Add(2, 6, "身份证号", xf2);
            sheet1.Cells.Add(2, 7, "工作单位", xf2);
            sheet1.Cells.Add(2, 8, "职务职称", xf2);
            sheet1.Cells.Add(2, 9, "联系方式", xf2);
            sheet1.Cells.Add(2, 10, "邮箱", xf2);
            sheet1.Cells.Add(2, 11, "是否有考官证", xf2);
            sheet1.Cells.Add(2, 12, "考官证获得日期", xf2);
            sheet1.Cells.Add(2, 13, "备注", xf2);
            ColumnInfo colInfoxueli1 = new ColumnInfo(xls, sheet1);
            colInfoxueli1.Width = 30 * 256;
            colInfoxueli1.ColumnIndexStart = 3;
            colInfoxueli1.ColumnIndexEnd = 3;
            sheet1.AddColumnInfo(colInfoxueli1);
            ColumnInfo colInfoxueli3 = new ColumnInfo(xls, sheet1);
            colInfoxueli3.Width = 25 * 256;
            colInfoxueli3.ColumnIndexStart = 5;
            colInfoxueli3.ColumnIndexEnd = 5;
            sheet1.AddColumnInfo(colInfoxueli3);
            ColumnInfo colInfoxueli9 = new ColumnInfo(xls, sheet1);
            colInfoxueli9.Width = 30 * 256;
            colInfoxueli9.ColumnIndexStart = 6;
            colInfoxueli9.ColumnIndexEnd = 6;
            sheet1.AddColumnInfo(colInfoxueli9);
            ColumnInfo colInfoxueli8 = new ColumnInfo(xls, sheet1);
            colInfoxueli8.Width = 20 * 256;
            colInfoxueli8.ColumnIndexStart = 7;
            colInfoxueli8.ColumnIndexEnd = 7;
            sheet1.AddColumnInfo(colInfoxueli8);
            ColumnInfo colInfoxueli6 = new ColumnInfo(xls, sheet1);
            colInfoxueli6.Width = 25 * 256;
            colInfoxueli6.ColumnIndexStart = 11;
            colInfoxueli6.ColumnIndexEnd = 11;
            sheet1.AddColumnInfo(colInfoxueli6);
            //填充内容    

            for (int i = 0; i < tempforall1.Count; i++)
            {
                sheet1.Cells.Add(i + 3, 1, i + 1, xf);
                if (tempforall1[i].XBType != null) sheet1.Cells.Add(i + 3, 2, tempforall1[i].XBType.ToString(), xf);
                else sheet1.Cells.Add(i + 3, 2, "", xf);
                if (tempforall1[i].KGType != null) sheet1.Cells.Add(i + 3, 3, tempforall1[i].KGType.ToString(), xf);
                else sheet1.Cells.Add(i + 3, 3, "", xf);
                if (tempforall1[i].YongRenDanWei != null) sheet1.Cells.Add(i + 3, 4, tempforall1[i].YongRenDanWei.ToString(), xf);
                else sheet1.Cells.Add(i + 3, 4, "", xf);
                if (tempforall1[i].Name != null) sheet1.Cells.Add(i + 3, 5, tempforall1[i].Name.ToString(), xf);
                else sheet1.Cells.Add(i + 3, 5, "", xf);
                if (tempforall1[i].CardNo != null) sheet1.Cells.Add(i + 3, 6, tempforall1[i].CardNo.ToString(), xf);
                else sheet1.Cells.Add(i + 3, 6, "", xf);
                if (tempforall1[i].GongZuoDanWei != null) sheet1.Cells.Add(i + 3, 7, tempforall1[i].GongZuoDanWei.ToString(), xf);
                else sheet1.Cells.Add(i + 3, 7, "", xf);
                if (tempforall1[i].ZhiWuZhiCheng != null) sheet1.Cells.Add(i + 3, 8, tempforall1[i].ZhiWuZhiCheng.ToString(), xf);
                else sheet1.Cells.Add(i + 3, 8, "", xf);
                if (tempforall1[i].LianXiFangShi != null) sheet1.Cells.Add(i + 3, 9, tempforall1[i].LianXiFangShi.ToString(), xf);
                else sheet1.Cells.Add(i + 3, 9, "", xf);
                if (tempforall1[i].EMail != null) sheet1.Cells.Add(i + 3, 10, tempforall1[i].EMail, xf);
                else sheet1.Cells.Add(i + 3, 10, "", xf);

                if (tempforall1[i].IsKaoGuanZhengS != null) sheet1.Cells.Add(i + 3, 11, tempforall1[i].IsKaoGuanZhengS.ToString(), xf);
                else sheet1.Cells.Add(i + 3, 11, "", xf);
                if (tempforall1[i].RiQi != null) sheet1.Cells.Add(i + 3, 12, Convert.ToDateTime(tempforall1[i].RiQi.ToString()).ToString("yyyy-MM-dd"), xf);
                else sheet1.Cells.Add(i + 3, 12, "", xf);
                if (tempforall1[i].Memo != null) sheet1.Cells.Add(i + 3, 13, tempforall1[i].Memo, xf);
                else sheet1.Cells.Add(i + 3, 13, "", xf);

            }
            xls.Save(ss);
            ss.Close();
            ss.Dispose();
            SilverlightMessageBoxLibrary.Message.InfoMessage("导出成功！");
        }
        /// <summary>
        /// 导出汇总表
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private Stream _Stream = null;
        private void ExportAll_Click(object sender, RoutedEventArgs e)
        {
            SaveFileDialog dlg = new SaveFileDialog()
            {
                Filter = "Excel文件 (*.xls)|*.xls"
            };

            if (!(bool)dlg.ShowDialog())
            {
                return;
            }
            try
            {
              
                _Stream = dlg.OpenFile();

            }
            catch
            {
                MessageBox.Show("其它程序正在使用该文件，请关闭使用该文件的程序后重试！");
                return;
            }

            Client.ExportKGAsync(name.Text, tiaojian,jhname);
            Client.ExportKGCompleted += new EventHandler<ExportKGCompletedEventArgs>(Client_ExportKGCompleted);

        }

        void Client_ExportKGCompleted(object sender, ExportKGCompletedEventArgs e)
        {
            try
            {
                byte[] buffer = e.Result;
                _Stream.Write(buffer, 0, buffer.Length);
                MessageBox.Show("导出完毕！");
                _Stream.Close();
                _Stream.Dispose();
                return;
            }
            catch
            {

            }
        }
        private ObservableCollection<PublicOfficialSelectionService.KaoGuanModel> selectedempmodel =
           new ObservableCollection<PublicOfficialSelectionService.KaoGuanModel>();
        private void CheckBox_Click(object sender, RoutedEventArgs e)
        {
            CheckBox cb = sender as CheckBox;
            foreach (KaoGuanModel p in DGRole.ItemsSource)
            {
                p.IsSelected = (bool)cb.IsChecked;
                //CheckBox cb1 = DGRole.Columns[0].GetCellContent(p) as CheckBox;
                //cb1.IsChecked = p.IsSelected;
                if (p.IsSelected)
                {
                    if (selectedempmodel.Where(i => i.ID == p.ID).FirstOrDefault() == null)
                    {
                        selectedempmodel.Add(p);
                    }
                }
                else
                {
                    if (selectedempmodel.Where(i => i.ID == p.ID).FirstOrDefault() != null)
                    {
                        selectedempmodel.Remove(selectedempmodel.Where(i => i.ID == p.ID).FirstOrDefault());
                    }
                }
            }

        }
    }
}
